package nc.tb.pub.ui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import nc.ui.plaf.basic.BasicSingleSideZoomPaneUI;
import nc.ui.pub.beans.SingleSideZoomPane;

public class TbSingleSideZoomPaneUI  extends BasicSingleSideZoomPaneUI {
	private Icon smallBarIcon = new ImageIcon(getClass().getClassLoader().getResource("images/ntbimage/bar_handle.gif"));
    private Icon smallBarIcon_h = new ImageIcon(getClass().getClassLoader().getResource("images/ntbimage/bar_handle_h.gif"));
    private Icon smallBarIcon_o = new ImageIcon(getClass().getClassLoader().getResource("images/ntbimage/bar_handle_o.gif"));
    private Icon smallBarIcon_ho = new ImageIcon(getClass().getClassLoader().getResource("images/ntbimage/bar_handle_ho.gif"));
    protected void paintSmallBar(Graphics g) {
        Graphics2D g2 = null;
        int h = getBarPanel().getHeight();
        int w = getBarPanel().getWidth();
        int barSize = singleSideZoomPane.getBarSize();
        int placement = singleSideZoomPane.getBarPlacement();
        try {
            g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            GradientPaint paint = null;
            if(singleSideZoomPane.isOpaque()){
                getGradientPaint(placement, barSize, Color.lightGray, Color.darkGray.brighter());//new GradientPaint(0, 0, Color.white.darker(), 0, h, Color.darkGray.brighter());
                g2.setPaint(paint);
                g2.fillRect(0, 0, w, h);
            }

            Rectangle rect = getButtonRectangle();
            boolean isOverBar = (mouseState == MOUSE_STATE_IN_BTN || mouseState == MOUSE_STATE_PRESS_BTN); 
            if(placement == SingleSideZoomPane.RIGHT || placement == SingleSideZoomPane.LEFT){
            	if(isOverBar)
            		smallBarIcon_o.paintIcon(singleSideZoomPane, g2, rect.x, rect.y);
            	else
            		smallBarIcon.paintIcon(singleSideZoomPane, g2, rect.x, rect.y);
            }else{
            	if(isOverBar)
            		smallBarIcon_ho.paintIcon(singleSideZoomPane, g2, rect.x, rect.y);
            	else
            		smallBarIcon_h.paintIcon(singleSideZoomPane, g2, rect.x, rect.y);
            }
        } finally {
            if (g2 != null) {
                g2.dispose();
            }
        }
    }
    protected Rectangle getButtonRectangle() {
    	int x = 0, y = 0, w = 0, h = 0;

    	int placement = singleSideZoomPane.getBarPlacement();
    	if(placement == SingleSideZoomPane.RIGHT){
	  		w = smallBarIcon.getIconWidth();
	  		h = smallBarIcon.getIconHeight();
    		y = (getBarPanel().getHeight() - h - 25) / 2;
    		x = (getBarPanel().getWidth() - w) / 2;
    	}else if(placement == SingleSideZoomPane.LEFT){
	  		w = smallBarIcon.getIconWidth();
	  		h = smallBarIcon.getIconHeight();
    		y = (getBarPanel().getHeight() - h - 25) / 2;
    		x = (getBarPanel().getWidth() - w) / 2;
    	}else{
    		w = smallBarIcon_h.getIconWidth();
    		h = smallBarIcon_h.getIconHeight();
    		y = (getBarPanel().getHeight() - h) / 2;
    		x = (getBarPanel().getWidth() - w) / 2;
    	}
    	
    	return new Rectangle(x, y, w, h);
    }
}
